<?php

namespace App\Middleware;

use App\Constant\ErrorCode;
use App\Model\User;
use App\Support\Response;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
 * 验证会员中间件
 */
class MemberMiddleware implements MiddlewareInterface
{
    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject()
     * @var Response
     */
    protected  $responsel;

    /**
     * @var HttpResponse
     */
    protected $response;

    /**
     * 授权验证守卫
     *
     * @var string
     */
    private $guard = 'jwt';

    public function __construct(HttpResponse $response, RequestInterface $request)
    {
        $this->response = $response;
        $this->request  = $request;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $guard = auth('jwt');

        $uid = $guard->user()->getId();


        $member = User::checkMember($uid);
        if ($member === false){
            return  $this->responsel->returnFail(1023);
        }


        return $handler->handle($request);
    }
}
